// packageC/pages/package/agv_manage/controlAreaManage/addControlArea/addControlArea.js
const mybehavior = require("../../../../../../behaviors/requests")
import WxValidate from "../../../../../../Wx_Validate/WxValidate"
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/agv_dispatch_system",
    department: "",
    workshop: "",
    caozuo_show: false,
    control_val: "",
    cz_key: "",
    area_name: "",
    cz_id: "",
    options_in: [],
    options_out: [],
    options_in_out: [],
    add_tag_show: true,
    map_list: [[], [], []],

    radio: "1",
    map_list_show: false,
  },
  // 新增管制区数据
  submit() {
    console.log(this.data);
    let url = this.data.baseUrl + "/add_control_area"
    let data = {
      area_coordinate: this.data.map_list,
      area_name: this.data.area_name,
      department: this.data.department,
      inlet_card_list: this.data.options_in,
      outlet_card_list: this.data.options_out,
      workshop: this.data.workshop,
    }
    console.log(data);
    if (!this.WxValidate.checkForm(data)) {
      const error = this.WxValidate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
    } else {
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          wx.showModal({
            title: res.msg,
            content: '',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                wx.navigateBack()

              }
            }
          })

        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }).catch(err => {

        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
    }


  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      department: {
        required: true,
      },
      // area_coordinate: {
      //   required: true,
      // },

      area_name: {
        required: true,
      },

      inlet_card_list: {
        required: true,
      },
      outlet_card_list: {
        required: true,
      },
      workshop: {
        required: true,
      },
   





    }

    let message = {
      department: {
        required: "部门-不能为空",
      },
      // area_coordinate: {
      //   required: "标签-不能为空",
      // },
      area_name: {
        required: "交管区名称-不能为空",
      },
      inlet_card_list: {
        required: "管制入口卡-不能为空",
      },
      outlet_card_list: {
        required: "管制出口卡-不能为空",
      },
      workshop: {
        required: "所属车间-不能为空",
      },
    




    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  // 选择是否圈画地图
  showMapChange(event) {
    if (event.detail == "2") {
      this.setData({
        radio: event.detail,
        map_list_show: true,
      });
    } else {
      this.setData({
        radio: event.detail,
        map_list_show: false,
      });
    }

  },
  // 查询部门信息
  getDepartmentData() {
    let url = this.data.baseUrl + "/get_department_list"

    let data = {
      current: 1,
      page_size: 999,
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options1: res.data,
        })

      } else {
        this.setData({
          options1: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options1: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 选择部门值
  getPickerVlaue1(e) {
    let key = e.target.id
    console.log(e);
    this.setData({
      [key]: e.detail

    })
    this.getWorkshopData(e.detail)
  },
  // 查询车间信息
  getWorkshopData(e) {
    let url = this.data.baseUrl + "/get_workshop_list"

    let data = {
      department: e
    }
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options2: res.data,
        })

      } else {
        this.setData({
          options2: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options2: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 选择车间信息值
  getPickerVlaue2(e) {
    let key = e.target.id
    console.log(e);
    this.setData({
      [key]: e.detail

    })
  },
  // 管制区出入口卡弹窗
  onClick(e) {
    let lists = JSON.parse(JSON.stringify(this.data[e.target.dataset.id]))
    console.log(lists);
    this.setData({
      caozuo_show: true,
      cz_key: e.target.dataset.key,
      cz_id: e.target.dataset.id,
      options_in_out: lists

    })
  },
  // 管制出入口卡弹窗添加按钮是否可选
  chechAddTagBtn() {
    if (this.data.control_val != "") {
      this.setData({
        add_tag_show: false
      })
    } else {
      this.setData({
        add_tag_show: true
      })
    }
  },
  // 添加管制出入口值
  addTag() {
    let lists = JSON.parse(JSON.stringify(this.data.options_in_out))
    if (lists.includes(Number(this.data.control_val))) {
      wx.showToast({
        title: '禁止重复添加！',
        icon: 'none'
      })
      this.setData({
        control_val: "",
      })
    } else {
      lists.push(Number(this.data.control_val))
      this.setData({
        options_in_out: lists,
        control_val: "",
        add_tag_show: true

      })
    }


  },

  // 删除管制出入口值
  deleteTag(e) {
    console.log(e.target.dataset.index);
    let lists = JSON.parse(JSON.stringify(this.data.options_in_out))
    lists.splice(e.target.dataset.index, 1)
    this.setData({
      options_in_out: lists,
    })
  },

  // 添加一行地图坐标数据
  addMapData() {
    let lists = this.data.map_list
    lists.push([])
    this.setData({
      map_list: lists,
    })
  },
  // 双向绑定地图坐标数据
  bindMapData(e) {
    console.log(e);
    let id_ = e.target.dataset.id
    let index = e.target.dataset.index
    let lists = this.data.map_list
    lists[index][id_] = e.detail
    this.setData({
      map_list: lists
    })
  },
  // 删除一行地图坐标数据
  deleteMapData(e) {
    let index = e.target.dataset.index
    let lists = this.data.map_list

    lists.splice(index, 1)
    this.setData({
      map_list: lists,
    })
  },
  // 管制出入口卡值确认
  onConfirm() {
    let key = this.data.cz_id
    let lists = JSON.parse(JSON.stringify(this.data.options_in_out))
    this.setData({
      [key]: lists,
      options_in_out: []
    })
  },
  // 取消添加管制出入口卡值
  onCancel() {
    this.setData({
      caozuo_show: false,
      options_in_out: [],
      control_val: "",
      cz_key: "",
      cz_id: "",
      add_tag_show: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()
    this.getDepartmentData()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})